package com.sunthy.mall.controller.admin;

import com.google.gson.Gson;
import com.sunthy.mall.model.admin.Result;
import com.sunthy.mall.model.admin.vo.admin.AllUserVO;
import com.sunthy.mall.service.admin.user.UserService;
import com.sunthy.mall.service.admin.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用来对User用户进行删查操作
 */
@WebServlet("/api/admin/user/*")
public class UserServlet extends HttpServlet {

    private Gson gson = new Gson();

    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String replace = requestURI.replace(request.getContextPath() + "/api/admin/user/", "");
        if ("allUser".equals(replace)) {

            // 查看所有的user
            allUser(request, response);

        }
        if ("deleteUser".equals(replace)) {

            // 删除user
            deleteUser(request, response);

        }
        if ("searchUser".equals(replace)) {

            // 搜索user
            searchUser(request, response);

        }
    }

    /**
     * 搜索的逻辑:根据word 查询user的nickname
     *        1.接受请求参数
     *        2.去数据库中查询
     *        3.返回结果给客户端
     *
     * @param request
     * @param response
     */
    private void searchUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收请求参数
        String nickname = request.getParameter("word");
        // 去数据库查询
        List<AllUserVO> allUserVOS = userService.searchAllUser(nickname);
        // 返回结果给客户端
        response.getWriter().println(gson.toJson(Result.ok(allUserVOS)));
    }

    /**
     * 删除user的逻辑:
     *        1.接收请求参数
     *        2.去数据库中删除
     *        3.返回结果给客户端
     *
     * @param request
     * @param response
     */
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收请求参数
        String idStr = request.getParameter("id");
        // 检验参数
        if (idStr == null) {
            response.getWriter().println(Result.error("参数错误"));
            return;
        }
        int id = Integer.parseInt(idStr);
        int code = userService.delete(id);
        if (code == 0) {// 删除成功
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }// 失败
        response.getWriter().println(gson.toJson(Result.error("删除异常")));
    }

    /**
     * 查看所有user的逻辑:
     *        1.去数据库中查找
     *        2.返回结果给客户端
     *
     * @param request
     * @param response
     */
    private void allUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<AllUserVO> adminVOS = userService.allUser();
        response.getWriter().println(gson.toJson(Result.ok(adminVOS)));
    }

}
